package com.wofish.goods.service.impl;

import java.util.*;

import com.wofish.goods.domain.Goods;
import com.wofish.goods.domain.Products;
import com.wofish.goods.dto.ProductsIndex;
import com.wofish.goods.dto.SpecObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wofish.goods.mapper.GoodsSpecIndexMapper;
import com.wofish.goods.domain.GoodsSpecIndex;
import com.wofish.goods.service.IGoodsSpecIndexService;
import com.wofish.common.core.text.Convert;

/**
 * 商品规格关系 服务层实现
 * 
 * @author wofish
 * @date 2019-06-16
 */
@Service
public class GoodsSpecIndexServiceImpl implements IGoodsSpecIndexService {
	@Autowired
	private GoodsSpecIndexMapper goodsSpecIndexMapper;


	@Override
	public List<Long> querySpecValueIds(Long goodsId, Long typeId, Long specId) {
		return goodsSpecIndexMapper.querySpecValueIds(goodsId,typeId,specId);
	}

	/**
     * 查询商品规格关系信息
     * 
     * @param typeId 商品规格关系ID
     * @return 商品规格关系信息
     */
    @Override
	public GoodsSpecIndex selectGoodsSpecIndexById(Long typeId) {
	    return goodsSpecIndexMapper.selectGoodsSpecIndexById(typeId);
	}
	
	/**
     * 查询商品规格关系列表
     * 
     * @param goodsSpecIndex 商品规格关系信息
     * @return 商品规格关系集合
     */
	@Override
	public List<GoodsSpecIndex> selectGoodsSpecIndexList(GoodsSpecIndex goodsSpecIndex) {
	    return goodsSpecIndexMapper.selectGoodsSpecIndexList(goodsSpecIndex);
	}
	
    /**
     * 新增商品规格关系
     * 
     * @param goodsSpecIndex 商品规格关系信息
     * @return 结果
     */
	@Override
	public int insertGoodsSpecIndex(GoodsSpecIndex goodsSpecIndex) {
	    return goodsSpecIndexMapper.insertGoodsSpecIndex(goodsSpecIndex);
	}
	
	/**
     * 修改商品规格关系
     * 
     * @param goodsSpecIndex 商品规格关系信息
     * @return 结果
     */
	@Override
	public int updateGoodsSpecIndex(GoodsSpecIndex goodsSpecIndex) {
	    return goodsSpecIndexMapper.updateGoodsSpecIndex(goodsSpecIndex);
	}

	/**
     * 删除商品规格关系对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteGoodsSpecIndexByIds(String ids) {
		return goodsSpecIndexMapper.deleteGoodsSpecIndexByIds(Convert.toStrArray(ids));
	}

	@Override
	public int deleteGoodsSpecIndexByGoodsId(Long goodsId) {
		return goodsSpecIndexMapper.deleteGoodsSpecIndexByGoodsId(goodsId);
	}

	@Override
	public int insertBatch(List<GoodsSpecIndex> list) {
		return goodsSpecIndexMapper.insertBatch(list);
	}

	@Override
	public List<GoodsSpecIndex> buildGoodsSpecIndexes(Long goodsId, Long typeId, List<ProductsIndex> productsIndexList, List<Products> productsList) {
		for (int i = 0; i < productsList.size(); i++) {
			productsIndexList.get(i).setProductId(productsList.get(i).getProductId());
		}

		Set<GoodsSpecIndex> goodsSpecIndexSet = new HashSet<>();
		GoodsSpecIndex model = null;
		for (ProductsIndex productsIndex : productsIndexList) {
			List<SpecObject> specObjects = productsIndex.getSpecJson();
			Long productId = productsIndex.getProductId();
			for (SpecObject specObject : specObjects) {
				model = new GoodsSpecIndex();
				model.setLastModify(new Date());
				model.setTypeId(goodsId);
				model.setGoodsId(typeId);
				model.setProductId(productId);
				model.setSpecId(specObject.getSpecId());
				model.setSpecValueId(specObject.getSpecValueId());
				model.setImageId(specObject.getImageId());
				model.setImageUrl(specObject.getImageUrl());
				goodsSpecIndexSet.add(model);
			}
		}
		return new ArrayList<>(goodsSpecIndexSet);
	}
}
